﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using BlueLotus7.Eden.Web.Properties;
using BlueLotus7.ArchiveSystem.Common;
using System.Web;
using System.Text;
using BlueLotus7.Common;
using BlueLotus7.Eden.Web.Models;
using BlueLotus7.Web;
using System.Web.Routing;

namespace BlueLotus7.Eden.Web
{
    public static class Helper
    {
        public static string ConvertToDisplayString(DateTime? stamp)
        {
            string result = string.Empty;
            if (stamp != null)
            {
                result = stamp.Value.ToString("yyyy-MM-dd HH:mm:ss");
            }
            return result;
        }

        public static string DateTimeToDateString(DateTime? stamp)
        {
            string result = string.Empty;
            if (stamp != null)
            {
                result = stamp.Value.ToString("yyyy-MM-dd");
            }
            return result;
        }

        public static string DateTimeToTimeString(DateTime? stamp)
        {
            string result = string.Empty;
            if (stamp != null)
            {
                result = stamp.Value.ToString("HH:mm:ss");
            }
            return result;
        }

        #region ConvertToDisplayString

        public static string ConvertToDisplayString(string value)
        {
            return string.IsNullOrWhiteSpace(value) ? string.Empty : value;
        }

        public static string ConvertToDisplayString(bool value)
        {
            return value ? "Yes" : "No";
        }

        public static string ConvertToDisplayString(object value)
        {
            return value == null ? string.Empty : value.ToString();
        }

        #endregion

        public static string ConvertToHtmlDisplayString(object value)
        {
            return HttpUtility.HtmlEncode(ConvertToDisplayString(value));
        }

        public static string MasterPageUrl
        {
            get
            {
                return string.Format("~/Views/Shared/{0}.cshtml", Settings.Default.MasterPage);
            }
        }

        public static Type GetStatusEnumTypeByCode(string code)
        {
            Type type = null;

            switch (code)
            {
                case "qq":
                    type = typeof(QQStatus);
                    break;
                default:
                    break;
            }

            return type;
        }

        public static string GenerateEntityJsonObject(object obj, string panelId)
        {
            StringBuilder scriptBuilder = new StringBuilder();

            if (!string.IsNullOrWhiteSpace(panelId))
            {
                string entityObjId = "entobj" + panelId;
                scriptBuilder.Append("<script type=\"text/javascript\"> ");
                scriptBuilder.Append("var " + entityObjId + " = " + (obj == null ? "undefined" : System.Web.Helpers.Json.Encode(obj)) + ";");
                scriptBuilder.Append("$(document).ready(function () {$('#" + panelId + "').data('original'," + entityObjId + ")});");
                scriptBuilder.Append("</script>");
            }

            return scriptBuilder.ToString();
        }

        public static string GeneratePanelId(string objectType, IIdentifiable obj)
        {
            string panelId = string.Empty;

            if (!string.IsNullOrWhiteSpace(objectType) && obj != null)
            {
                panelId = objectType + "_" + ((obj == null || Validator.IsZeroGuid(obj.Key)) ? "new" : (obj.Key.ToString().Replace("-", "_")));
            }

            return panelId;
        }


        #region Form

        public static string GenerateFormSpanHtml(ViewMode viewMode, string text, string value)
        {
            string result = string.Empty;

            switch (viewMode)
            {
                case ViewMode.Editable:
                case ViewMode.EditOnDefault:

                    break;
                case ViewMode.Readonly:
                default:
                    break;
            }

            return result;
        }

        public static string GenerateFormFormFieldHtml(ViewMode viewMode, string fieldKey, string text, string value, string type)
        {
            string result = string.Empty;

            switch (viewMode)
            {
                case ViewMode.Editable:
                case ViewMode.EditOnDefault:

                    break;
                case ViewMode.Readonly:
                default:
                    break;
            }

            return result;
        }

        #endregion

        #region Utiltity

        public static PageRouteInfo GetPageRouteInfo(RequestContext requestContext)
        {
            PageRouteInfo info = null;

            if (requestContext != null)
            {
                info = new PageRouteInfo();
                RouteValueDictionary dictionary = requestContext.RouteData.DataTokens;
                object tempValue = dictionary["area"];
                if (tempValue != null)
                {
                    info.Area = tempValue.ToString().Trim();
                }

                tempValue = dictionary["action"];
                if (tempValue != null)
                {
                    info.Action = tempValue.ToString().Trim();
                }

                tempValue = dictionary["controller"];
                if (tempValue != null)
                {
                    info.Controller = tempValue.ToString().Trim();
                }
            }

            return info;
        }

        public static PageRouteInfo GetPageRouteInfo(BaseController controller)
        {
            PageRouteInfo info = null;

            if (controller != null)
            {
                info = GetPageRouteInfo(controller.ControllerContext.RequestContext);
            }

            return info;
        }

        #endregion

    }
}
